//
//  RJWarningDayView.m
//  AirQuality
//
//  Created by Po on 15/4/8.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningDay.h"
#import "RJTools.h"
#import "CALayer+MBorderColor.h"
@interface RJWarningDay () <UITextFieldDelegate>

- (void)initEvent;
@end

@implementation RJWarningDay

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"RJWarningDayView" owner:self options:nil];
        UIView * view = array[0];
        [view setFrame:self.bounds];
        [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"alphaBack"]]];
        [self addSubview:view];
        [self initEvent];
        NSArray * labelArray = @[_timeLabel,_warningTitleLabel, _hour48Label, _hour48Value, _hour72Label, _hour72Value, _hour96Label, _hour96Value,
                                 _concentrationLabel,_concentrationRankTitleLabel,_concentrationTitleLabel,_concentrationValueLabel,
                                 _AQILabel, _aqiRankTitleLabel];
        for (UILabel * label in labelArray) {
            [label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"homeTableCellBack"]]];
        }
        
        [_textFiled setBackgroundColor:[UIColor clearColor]];
        [_textFiled setValue:GetRGBColor(255, 255, 255, 0.5) forKeyPath:@"_placeholderLabel.textColor"];
        [_textFiled setBorderStyle:UITextBorderStyleNone];
        
    }
    return self;
}

- (void)initEvent
{
    [_textFiled setDelegate:self];
}

#pragma mark - UITextFiledDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGFloat yPoint = CGRectGetMaxY(textField.frame) + self.frame.origin.y + textField.frame.size.height + 10;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WarningTextShow" object:nil userInfo:@{@"textBottomY":@(yPoint)}];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@""]) {
        return YES;
    }
    
    if([textField.text length] >= 3){
        return NO;
    }
    
    NSRange mrange = [textField.text rangeOfString:@"."];
    
    //约束0的个数
    if ([string isEqualToString:@"0"] && mrange.location == NSNotFound) {
        NSString * text = [NSString stringWithFormat:@"%@0",textField.text];
        NSNumber * num = @([text floatValue]);
        [textField setText:GetStringWithNum(num)];
        return NO;
    }
    
    if ([string isEqualToString:@"."]) {
        //自动补0
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"0";
            return YES;
        }
        //限制小数点
        if (mrange.location != NSNotFound && (range.location != mrange.location)) {
            return NO;
        } else {
            return YES;
        }
        return YES;
    }
    
    //限制输入为数字
    if ([RJTools isNumberString:string] || [string isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WarningTextHide" object:nil userInfo:nil];
    [textField endEditing:YES];
    if (_delegate) {
        [_delegate WarningDay:self number:[textField.text floatValue]];
    }
    return YES;
}

@end
